﻿#include "tabediter.h"
#include "ui_tabediter.h"
#include <QFile>

TabEditer::TabEditer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TabEditer)
{
    ui->setupUi(this);
}

TabEditer::~TabEditer()
{
    delete ui;
}

QString TabEditer::text() const
{
    return ui->textEdit->toPlainText();
}

void TabEditer::setText(const QString &text)
{
    ui->textEdit->setText(text);
}


QString TabEditer::fileName() const
{
    return m_fileName;
}

void TabEditer::setFileName(const QString &name)
{
    if(m_fileName != name) {
        m_fileName = name;
    }
}

QString TabEditer::filePath() const
{
    return m_filePath;
}

void TabEditer::setFilePath(const QString &path)
{
    if(m_filePath.isEmpty()) {
        m_filePath = path;

        m_fileName = path.right(path.size() - path.lastIndexOf('/') - 1);
        m_fileName.replace(".json", "");

        readCocument(path);
    }
}

void TabEditer::readCocument(const QString &fileName)
{
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly)) {
        ui->textEdit->setText(file.readAll());
        file.close();
    }
}

void TabEditer::saveDocument(const QString &fileName)
{
    if(m_filePath.isEmpty())
        m_filePath = fileName;
    QFile file(fileName);
    if(file.open(QIODevice::WriteOnly)) {
        file.write(ui->textEdit->toPlainText().toUtf8());
        file.close();
    }
    else {
    }
}


